﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_Produse : System.Web.UI.Page
{
    StoreApplicationService sas = new StoreApplicationService();
    protected void Page_Load(object sender, EventArgs e)
    {
   
            var rawCategoryID = Page.RouteData.Values["categoryID"] as string;
            var rawBrandID = Page.RouteData.Values["brandID"] as string;

            int categoryID;
            int brandID;

            IList<Store.DateModel.ProductView> products = null;

            if (!String.IsNullOrEmpty(rawCategoryID) && Int32.TryParse(rawCategoryID, out categoryID))
            {
                products = sas.GetProductsByCategory(categoryID);
            }
            else if (!String.IsNullOrEmpty(rawBrandID) && Int32.TryParse(rawBrandID, out brandID))
            {
                products = sas.GetProductsByBrand(brandID);
            }
            else
            {
                products = sas.GetAllProducts();
            }
            ProductsRepeter.DataSource = products;
            ProductsRepeter.DataBind();
        
    }
    public void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        var pager = (ASPnetControls.PagerV2_8)UpdatePanel1.TemplateControl.FindControl("Pager");
        //var pager = Pager;
        pager.CurrentIndex = currnetPageIndx;
        BindRepeater();
    }

    private void BindRepeater()
    {

        var pager = (ASPnetControls.PagerV2_8)UpdatePanel1.FindControl("Pager");
        //var pager = Pager;
        int skip = (pager.CurrentIndex - 1) * pager.PageSize;
        int take = pager.PageSize;


        var rawCategoryID = Page.RouteData.Values["categoryID"] as string;
        var rawBrandID = Page.RouteData.Values["brandID"] as string;

        int categoryID;
        int brandID;

        IList<Store.DateModel.ProductView> products = null;

        if (!String.IsNullOrEmpty(rawCategoryID) && Int32.TryParse(rawCategoryID, out categoryID))
        {
            products = sas.GetProductsByCategory(categoryID);
        }
        else if (!String.IsNullOrEmpty(rawBrandID) && Int32.TryParse(rawBrandID, out brandID))
        {
            products = sas.GetProductsByBrand(brandID);
        }
        else
        {
            products = sas.GetAllProducts();
        }




        IEnumerable<Store.DateModel.ProductView> results = products.Skip(skip).Take(take);

        pager.ItemCount = products.Count();

        ProductsRepeter.DataSource = results;
        ProductsRepeter.DataBind();


    }
}